﻿package com.tudou.home.model
{
    import com.adobe.serialization.json.*;
    import com.tudou.home.commands.*;
    import com.tudou.js.*;
    import com.tudou.utils.*;
    import flash.events.*;
    import flash.net.*;
    import org.puremvc.as3.patterns.proxy.*;

    public class TollVideoProxy extends Proxy
    {
        private var loader:URLLoader;
        private var result:Object;
        private var pageInfo:PageInfo;
        private static const VERIFY_URL:String = "http://payment.tudou.com/payment/srv/hasPrivilege.html";
        public static const NAME:String = "TollVideoProxy";

        public function TollVideoProxy()
        {
            super(NAME);
            pageInfo = PageInfo.getInstance();
            JSExchanger.getInst().addCallback(this, "checkPay");
            return;
        }// end function

        public function checkPay() : void
        {
            verify();
            return;
        }// end function

        private function verifyHlr(event:Event) : void
        {
            var e:* = event;
            try
            {
                result = JSON.decode(loader.data);
                if (result.system == 1)
                {
                    pageInfo.password = result.itemPw;
                    sendNotification(TollVideoCommandSet.VERIFY_SUCCEED);
                }
                else
                {
                    sendNotification(TollVideoCommandSet.VERIFY_FAILED);
                }
            }
            catch (error:Error)
            {
                trace(error);
                sendNotification(TollVideoCommandSet.VERIFY_FAILED);
            }
            loader = URLLoaderUtil.deleteIt(loader, verifyHlr, otherErrorHlr, otherErrorHlr);
            return;
        }// end function

        private function otherErrorHlr(event:Event) : void
        {
            trace(event);
            return;
        }// end function

        public function get tollResult() : Object
        {
            return result;
        }// end function

        public function verify() : void
        {
            loader = URLLoaderUtil.newIt(loader, verifyHlr, otherErrorHlr, otherErrorHlr);
            var _loc_1:* = VERIFY_URL + "?itemID=" + pageInfo.iid + "&albumID=" + pageInfo.aid;
            loader.load(new URLRequest(_loc_1));
            return;
        }// end function

    }
}
